package com.hp.note;

import java.text.AttributedCharacterIterator.Attribute;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;



public class XMLHandler extends DefaultHandler {

	String elementValue = null;
	Boolean elementOn = false;
	public static XMLGettersSetters data = null;

	public static XMLGettersSetters getXMLData() {
		return data;
	}

	public static void setXMLData(XMLGettersSetters data) {
		XMLHandler.data = data;
	}

	/** 
	 * This will be called when the tags of the XML starts.
	 **/
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {

		elementOn = true;

		if(localName.equals("current_observation")){
			
			data = new XMLGettersSetters();
			 
		}
		
		
			/** 
			 * We can get the values of attributes for eg. if the CD tag had an attribute( <CD attr= "band">Akon</CD> ) 
			 * we can get the value "band". Below is an example of how to achieve this.
			 * 
			 * String attributeValue = attributes.getValue("attr");
			 * data.setAttribute(attributeValue);
			 * 
			 * */
		
	}

	/** 
	 * This will be called when the tags of the XML end.
	 **/
	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {

		elementOn = false;

		/** 
		 * Sets the values after retrieving the values from the XML tags
		 * */ 
		if (localName.equalsIgnoreCase("local_time"))
			data.setDate(elementValue);
		else if (localName.equalsIgnoreCase("relative_humidity"))
			data.setHumidity(elementValue);
		else if (localName.equalsIgnoreCase("temp_c"))
			data.setTempC(elementValue);
		else if (localName.equalsIgnoreCase("temp_f"))
			data.setTempF(elementValue);
		else if(localName.equalsIgnoreCase("weather"))
			data.setCondition(elementValue); 
		else if(localName.equalsIgnoreCase("city"))
			data.setNamecity(elementValue);	
		else if(localName.equals("icon_url"))	
		     data.setIcon(elementValue);	
	}

	/** 
	 * This is called to get the tags value
	 **/
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {

		if (elementOn) {
			elementValue = new String(ch, start, length);
			elementOn = false;
		}

	}

}
